package com.mzy.linear_struct.面试150题;

/**
 * @Author: mizy3
 * @Date: 2024/08/14/下午10:38
 * @Description:
 */
public class Pro169多数元素 {
    public int majorityElement(int[] nums) {
        int votes = 0, num;

        num = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if(num == nums[i]){
                votes++;
            }else if(votes == 0){
                num = nums[i];
            }else {
                votes--;
            }
        }

        return num;
    }
}
